#include <iostream>
#include "LogicUserBase.h"
#include <stdexcept>
#include <cstdio>

using namespace std;

LogicUserBase * LogicUserBase::plogic = NULL;

LogicUserBase::LogicUserBase()
{
	mysqlutil = NULL;

	Init();
}

LogicUserBase::~LogicUserBase()
{
	mysqlutil->CloseDB();

	delete mysqlutil;
}

LogicUserBase * LogicUserBase::GetInstance()
{
	if (NULL == plogic)
	{
		plogic = new LogicUserBase;
	}

	return plogic;
}

bool LogicUserBase::Init()
{
	mysqlutil = new MysqlUtil;

	//连接数据库
	bool connectok = mysqlutil->ConnectDB("localhost", 3306, "root", "123456", "tool");

	if (!connectok)
	{
		throw runtime_error("connect db error");
	}

	return true;
}

int LogicUserBase::GetBase(const char * openid, DataBase & base)
{
	char szsql[512] = {0};
	snprintf(szsql, sizeof(szsql), "select openid, uid, score, hit, name, level from base where openid='%s'", openid);

	int ret = mysqlutil->ExecSql(szsql);

	if (ret)
	{
		return 1;
	}

	vector<vector<string> > results;
	mysqlutil->FetchRows(results);

	if (0 == results.size())
	{
		return 2;
	}

	SetBase(results[0], base);

	return 0;
}

int LogicUserBase::GetBase(unsigned uid, DataBase & base)
{
	char szsql[512] = {0};
	snprintf(szsql, sizeof(szsql), "select openid, uid, score, hit, name, level from base where uid=%u", uid);

	int ret = mysqlutil->ExecSql(szsql);

	if (ret)
	{
		return 1;
	}

	vector<vector<string> > results;
	mysqlutil->FetchRows(results);

	if (0 == results.size())
	{
		return 2;
	}

	SetBase(results[0], base);

	return 0;
}

int LogicUserBase::GetBaseLimit(unsigned uid, DataBase & base)
{
	char szsql[512] = {0};
	snprintf(szsql, sizeof(szsql),"select uid, score, hit, level from base where uid=%u", uid);

	int ret = mysqlutil->ExecSql(szsql);

	if (ret)
	{
		return 1;
	}

	vector<vector<string> > results;
	mysqlutil->FetchRows(results);

	if (0 == results.size())
	{
		return 2;
	}

	SetBaseLimit(results[0], base);

	return 0;
}

int LogicUserBase::ReplaceIntoBase(DataBase & base)
{
	char szsql[512] = {0};
	snprintf(szsql, sizeof(szsql), "replace into base(openid, uid, score, hit) values('%s', %u,%u,%u)",
			base.openid, base.uid, base.score, base.hit);

	int ret = mysqlutil->ExecSql(szsql);

	if (ret)
	{
		return 1;
	}

	return 0;
}

int LogicUserBase::UpdateBaseLimit(DataBase & base)
{
	char szsql[512] = {0};
	sprintf(szsql, "update base set hit=%u, score=%u, level=%u where uid=%u",
			base.hit, base.score, base.level, base.uid);

	int ret = mysqlutil->ExecSql(szsql);

	if (ret)
	{
		return 1;
	}

	return 0;
}

void LogicUserBase::SetBase(vector<string> & dbdata, DataBase & base)
{
	snprintf(base.openid, 32, "%s", dbdata[0].c_str());

	base.uid = atoi(dbdata[1].c_str());
	base.score = atoi(dbdata[2].c_str());
	base.hit = atoi(dbdata[3].c_str());
	base.level = atoi(dbdata[5].c_str());
	snprintf(base.name, 32, "%s", dbdata[4].c_str());
}

void LogicUserBase::SetBaseLimit(vector<string> & dbdata, DataBase & base)
{
	// uid, score, hit, level
	base.uid = atoi(dbdata[0].c_str());
	base.score = atoi(dbdata[1].c_str());
	base.hit = atoi(dbdata[2].c_str());
	base.level = atoi(dbdata[3].c_str());
}
